<?php


namespace addons\shopro\model;


use app\api\model\LogModel;
use think\Db;
use think\Model;

class UserServe extends Model
{
    // 表名,不含前缀
    protected $name = 'shopro_user_coupons_serve';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    //删除过期服务券
    public static function delServe()
    {
        $list = UserServe::where('expiretime',"<", time())
            ->where('serve_num',">",0)
            ->whereNotNull('expiretime')
            ->select();
        foreach ($list as $val){
            $res = UserServe::get($val['id']);
            $log = [
                'user_id' => $res['user_id'],
                'log' => $res['serve_num'].'-服务礼遇券删除',
                'type' => 'coupons_expire',
            ];
            $insert = [
                "user_id" => $res['user_id'],
                "serve_id" => $val['id'],
                "serve_num" => $res['serve_num'],
                "expiretime" => $res['expiretime'],
                'createtime' => time(),
            ];
            Db::table('fa_shopro_user_coupons_serve_overdue')->insert($insert);
            LogModel::create($log);
            $res->serve_num = 0;
            $res->save();
        }
    }

    //删除过期兑换券
    public static function delCoupons()
    {
        $list = UserCoupons::where('expiretime',"<", time())
            ->where('coupons_num','>',0)
            ->whereNotNull('expiretime')
            ->select();
        foreach ($list as $v){
            $res = UserCoupons::get($v['id']);

            $log = [
                'user_id' => $res['user_id'],
                'log' => $res['coupons_num'].'-兑换券删除',
            ];
            LogModel::create($log);
            $res->coupons_num = 0;
            $res->save();
        }
    }

    //用户升级处理逻辑
    public static function userLevel($user_id)
    {
        self::feeServe($user_id);//发放服务礼遇券
        self::sendRuhui($user_id,2);//发放入会礼遇
        self::sendJidu($user_id);//发放季度礼遇券
        self::sendYear($user_id);//发放年终福包券
    }

    //发放季度礼遇券
    /**
     * 钻石 1年 4 次季度礼遇
     * 星耀 1年 4 次季度礼遇
     */
    public static function sendJidu($user_id)
    {
        //等级大于白银 ，每月一张10元抵扣券
        $user = \app\common\model\User::get($user_id);
        if($user['member_level'] < 4){
            return false;
        }
        $log = [
            'user_id' => $user_id,
            'date_text' => date('Y'),
            'log' => '季度礼遇券',
        ];
        $log = LogModel::where($log)->find();
        if($log){
            return false;
        }
        $log = [
            'user_id' => $user_id,
            'date_text' => date('Y'),
            'log' => '季度礼遇券',
        ];
        LogModel::create($log);
        UserCoupons::send($user_id,5,4);
    }

    //发放年终福包券
    /**
     * 黄金 年终福包礼遇 1次
     * 钻石 年终福包礼遇 2次
     * 星耀 年终福包礼遇 4次
     */
    public static function sendYear($user_id)
    {
        $user = \app\common\model\User::get($user_id);
        if($user['member_level'] < 3){
            return false;
        }
        $num = 1;
        if($user['member_level'] == 4){
            $num = 2;
        }
        if($user['member_level'] == 5){
            $num = 4;
        }
        $log = [
            'user_id' => $user_id,
            'date_text' => date('Y'),
            'level' => $user['member_level'],
            'log' => '年终福包礼遇券',
        ];
        $log = LogModel::where($log)->order('id desc')->find();
        if($log){
            return false;
        }
        if($user['member_level'] == 4){
            $log = [
                'user_id' => $user_id,
                'date_text' => date('Y'),
                'level' => 3,
                'log' => '年终福包礼遇券',
            ];
            $log = LogModel::where($log)->order('id desc')->find();
            if($log){
                $num = 1;
            }
        }
        if($user['member_level'] == 5){
            $log = [
                'user_id' => $user_id,
                'date_text' => date('Y'),
                'level' => 4,
                'log' => '年终福包礼遇券',
            ];
            $log = LogModel::where($log)->order('id desc')->find();
            if($log){
                $num = 2;
            }
        }
        $log = [
            'user_id' => $user_id,
            'date_text' => date('Y'),
            'level' => $user['member_level'],
            'log' => '年终福包礼遇券',
        ];
        LogModel::create($log);
        UserCoupons::send($user_id,6,$num);
    }

    //定时任务发放生日礼券
    public static function cronBirth()
    {
        $userList = \app\common\model\User::where('member_level','>=',2)
            ->whereNotNull('birthday')
            ->select();
        foreach($userList as $v){
            self::sendBirth($v);
        }
    }

    public static function sendBirth($user)
    {
        //等级大于白银 ，每月一张10元抵扣券
        $user_id = $user['id'];
        if($user['member_level'] < 2){
            return  false;
        }
        $birth = date('m-d',strtotime($user['birthday']));
        $date = date('m-d');
        if($birth != $date){
            return false;
        }
        $log = [
            'user_id' => $user_id,
            'date_text' => date('Y-m-d'),
            'log' => '生日券',
        ];
        $log = LogModel::where($log)->find();
        if($log){
            return false;
        }
        $log = [
            'user_id' => $user_id,
            'date_text' => date('Y-m-d'),
            'log' => '生日券',
        ];
        LogModel::create($log);

        $map = [
            'coupons_id' => 4,
            'user_id' => $user_id,
        ];
        $row = UserCoupons::where($map)->find();
        if($row){
            $row->coupons_num += 1;
            $row->expiretime = month_last();
            $row->save();
            return true;
        }
        $map = [
            'coupons_id' => 4,
            'user_id' => $user_id,
            'coupons_num' => 1,
            'expiretime' => month_last(),
        ];
        UserCoupons::create($map);
        return true;
    }

    //定时发放
    //月津贴和10月抵扣券
    public static function cronTime()
    {
        $d1 = date('Y-m-d');

        $d2 = month_last(true); //本月最后一天
        if($d1 == $d2){
            $userList = \app\common\model\User::where('member_level','>=',3)
                ->select();
            foreach($userList as $v){
                self::sendDikou($v['id']);
                self::sendCash($v['id']);
            }
        }
    }

    //发放每个月固定80元奖励
    public static function sendCash($user_id)
    {
        $user = \app\common\model\User::get($user_id);
        if($user['member_level'] < 3){
            return false;
        }
        $cash = 80;
        $text = '会员津贴';
        if($user['member_level'] == 4){
            $cash = 150;
        }
        if($user['member_level'] == 5){
            $cash = 450;
        }
        $log = [
            'user_id' => $user_id,
            'date_text' => date('Y-m'),
            'log' => $cash.'现金津贴',
        ];
        $log = LogModel::where($log)->find();
        if($log){
            return false;
        }
        $log = [
            'user_id' => $user_id,
            'date_text' => date('Y-m'),
            'log' => $cash.'现金津贴',
        ];
        LogModel::create($log);
        User::money($cash,$user,'cash_reward',0,$text);
    }

    //发放10元抵扣券
    public static function sendDikou($user_id)
    {
        //等级大于白银 ，每月一张10元抵扣券
        $user = \app\common\model\User::get($user_id);
        if($user['member_level'] < 2){
            return  false;
        }
        $log = [
            'user_id' => $user_id,
            'date_text' => date('Y-m'),
            'log' => '10元抵扣券',
        ];
        $log = LogModel::where($log)->find();
        if($log){
            return false;
        }
        $log = [
            'user_id' => $user_id,
            'date_text' => date('Y-m'),
            'log' => '10元抵扣券',
        ];
        LogModel::create($log);
        UserCoupons::send($user_id,3,1);
        return true;
    }





    //$coupons_id = 1 发放入会19.9体验券
    //$coupons_id = 2 发放入会礼遇
    public static function sendRuhui($user_id,$coupons_id = 1)
    {
        return UserCoupons::send($user_id,$coupons_id,1);
    }



    //会员发放服务礼遇券
    public static function feeServe($user_id)
    {
        $user = User::get($user_id);
        //钻石  1次
        if($user['member_level'] == 4){
            $log = [
                'user_id' => $user_id,
                'serve_type' => 2,
            ];
            $row = self::where($log)->find();
            if($row){
                $row->serve_num += 1;
                $row->save();
            }else{
                $log = [
                    'user_id' => $user_id,
                    'serve_type' => 2,
                    'expiretime' => year_last(),
                    'serve_name' => '会员专属服务',
                    'serve_num' => 1,
                ];
                self::create($log);
            }
        }

        //星耀  3次
        if($user['member_level'] == 5){
            $log = [
                'user_id' => $user_id,
                'serve_type' => 2,
            ];
            $row = self::where($log)->find();
            if($row){
                $row->serve_num += 3;
                $row->expiretime = null;
                $row->save();
            }else{
                $log = [
                    'user_id' => $user_id,
                    'serve_type' => 1,
                    //'expiretime' => year_last(),
                    'serve_name' => '会员专属服务',
                    'serve_num' => 3,
                ];
                self::create($log);
            }
        }
    }

    public static function send($user_id,$num)
    {
        $log = [
            'user_id' => $user_id,
            'serve_type' => 2,
        ];
        $row = self::where($log)->find();
        if($row){
            $row->serve_num += 1;
            $row->save();
        }else{
            $log = [
                'user_id' => $user_id,
                'serve_type' => 1,
                'expiretime' => year_last(),
                'serve_name' => '会员专属服务',
            ];
            self::create($log);
        }

    }

    //购买颜护系列商品确认收获后赠送服务券
    public static function sendCoupons($order_id)
    {
        //设置过期时间
        //颜护系列，才有服务卡券
        $items = OrderItem::where('order_id',$order_id)->select();
        //29.9不赠送服务券
        if (count($items) == 1){
            $order = Order::where('id', $order_id)->find();
            if (!empty($order) && !empty($order['coupons_id'])){
                $coupons = UserCoupons::get($order['coupons_id']);
                if (!empty($coupons) && $coupons['coupons_id'] == 1 && $order['pay_fee'] == 29.9){
                    return false;
                }
            }
        }
        foreach ($items as $val){
            $goods = Goods::get($val['goods_id']);
            if($goods){
                if(strpos($goods['category_ids'],',') !== false){
                    $cate_id = trim(strstr($goods['category_ids'],','),',');
                }else{
                    $cate_id = $goods['category_ids'];
                }
                $c_arr = [2,15,16];
                //购买颜护系列，送颜护服务卡券;
                if(in_array($cate_id,$c_arr)){
                    $log2 = [
                        'user_id' => $val['user_id'],
                        'use_order_id' => $order_id,
                    ];
                    $res = UserServeLog::where($log2)->find();
                    if($res){
                        return false;
                    }
                    $log = [
                        'user_id' => $val['user_id'],
                        'serve_type' => 1,
                        'cate_id' => $goods['category_ids']
                    ];
                    $row = self::where($log)->find();
                    if($row){
                        $row->serve_num += 1;
                        $row->use_order_id .= ','.$order_id;
                        $row->save();
                    }else{
                        $log = [
                            'user_id' => $val['user_id'],
                            'serve_type' => 1,
                            'expiretime' => year_last(),
                            'serve_name' => $goods['title'].'服务',
                            'cate_id' => $goods['category_ids'],
                            'use_order_id' => $order_id,
                        ];
                        self::create($log);
                        $log2 = [
                            'user_id' => $val['user_id'],
                            'use_order_id' => $order_id,
                        ];
                        UserServeLog::create($log2);
                    }
                }
            }
        }
    }

}
